const td = translate("公共模块");

export function buildTree(data: Array<any>, parentId = "0") {
  const result = [];
  for (const item of data) {
    if (item.parentId === parentId) {
      // item.menuName
      item.menuName = `${td.value[item.menuName] || item.menuName}`;
      item.children = buildTree(data, item.menuId);
      if (item.children.length === 0) {
        item.children = [];
      }
      result.push(item);
    }
  }
  return result;
}
export const findParentNodes = (nodes: any[], targetId: string, idAlias = "path") => {
  const findParent = (node: any, targetId: string, path: string[]): string[] => {
    if (node[idAlias] === targetId) {
      return path;
    }
    path.push(node[idAlias]);

    if (node.children) {
      for (let child of node.children) {
        return findParent(child, targetId, path);
      }
    }
    return [];
  };
  for (let node of nodes) {
    return findParent(node, targetId, []);
  }
  return [];
};
